package com.cheetah.start.monitor.apicount.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.monitor.apicount.domain.SysApiCount;
import com.cheetah.start.monitor.apicount.mapper.SysApiCountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * sys api服务计数
 *
 * @author Feel
 * @date 2021/08/29
 */
@Slf4j
@Service
public class SysApiCountServiceImpl extends ServiceImpl<SysApiCountMapper, SysApiCount> implements SysApiCountService {

    @Async
    @Override
    public void record(SysApiCount sysApiCount) {
        QueryWrapper<SysApiCount> wrapper = new QueryWrapper<>();
        wrapper.eq("api", sysApiCount.getApi())
                .eq("api_day", sysApiCount.getApiDay());
        SysApiCount one = getOne(wrapper);
        if (one == null) {
            save(sysApiCount);
        } else {
            Integer apiCount = one.getApiCount();
            one.setApiCount(apiCount + 1);
            one.setUpdateTime(new Date());
            updateById(one);
        }
    }
}
